package com.siemens.industrial.web.mvc.service.admin;

import com.siemens.industrial.model.admin.SystemConfig;
import com.siemens.industrial.utils.EmailUtil;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.JsonToOtherFormat;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.dao.admin.SystemConfigDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.Map;

/**
 * Created by qupen on 2016/9/22.
 */
@Service
public class SystemConfigService {
    @Resource
    SystemConfigDao systemConfigDao;

    public SystemConfig getOneByConfigKey(String key) {
        return systemConfigDao.findOneByKey(key);
    }

    public void mailConfig(String key, Object data) {
        SystemConfig mailConfig = this.getOneByConfigKey(key);
        if (mailConfig != null) {
            mailConfig.setSystemName("mail");
            mailConfig.setSystemValue(data == null ? null : data.toString());
            systemConfigDao.updateByKey(mailConfig);
        } else {
            SystemConfig mail_config = new SystemConfig();
            mail_config.setSystemKey(SystemConfig.Mail_CONFIG_KEY);
            mail_config.setSystemName("mail");
            mail_config.setSystemValue(data == null ? null : data.toString());
            systemConfigDao.save(mail_config);
        }
    }

    public void logsConfig(String key, Object data) {
        SystemConfig logsConfig = this.getOneByConfigKey(key);
        if (logsConfig != null)
            systemConfigDao.deleteByKey(key);
        SystemConfig logs = new SystemConfig();
        logs.setSystemKey(SystemConfig.LOGS_CONFIG_KEY);
        logs.setSystemName("logs");
        logs.setSystemValue(JsonToOtherFormat.buildJsonToString(data));
        systemConfigDao.save(logs);
    }

    public int setDBBackup(String key, String data) {
        int i;
        SystemConfig systemConfig = new SystemConfig();
        systemConfig.setSystemKey(key);
        systemConfig.setSystemName(key);
        systemConfig.setSystemValue(data);
        if (getOneByConfigKey(key) == null) {
            i = systemConfigDao.save(systemConfig);
        } else {
            i = systemConfigDao.updateByKey(systemConfig);
        }
        return i;
    }

    public Map<String,Object> sendMail(String receiveMail,String subject,String content){
        SystemConfig systemConfig = this.getOneByConfigKey(SystemConfig.Mail_CONFIG_KEY);
        if (systemConfig == null)
            return RestResponse.create(GlobalResponseCode.ERROR_MAIL_NOT_SERVER_SETTINGS).build();

        Map<String, String> mailInfo = EmailUtil.buildMailProperties(systemConfig.getSystemValue());
        String[] receivers = {receiveMail};
        try {
            EmailUtil emailUtil = new EmailUtil(mailInfo);
            emailUtil.setMailParam(subject, receivers, content, mailInfo.get("username"));
            emailUtil.send();
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        } catch (MessagingException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.ERROR_MAIL_SEND_FAIL).build();//邮件发送失败
        }
    }

    public Map<String,Object> sendMail(String[] receiveMail,String subject,String content){
        SystemConfig systemConfig = this.getOneByConfigKey(SystemConfig.Mail_CONFIG_KEY);
        if (systemConfig == null)
            return RestResponse.create(GlobalResponseCode.ERROR_MAIL_NOT_SERVER_SETTINGS).build();

        Map<String, String> mailInfo = EmailUtil.buildMailProperties(systemConfig.getSystemValue());
        try {
            EmailUtil emailUtil = new EmailUtil(mailInfo);
            emailUtil.setMailParam(subject, receiveMail, content, mailInfo.get("username"));
            emailUtil.send();
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        } catch (MessagingException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.ERROR_MAIL_SEND_FAIL).build();//邮件发送失败
        }
    }
}
